package com.gsm.service.impl;

import com.gsm.dao.RegisterDao;
import com.gsm.dao.WebUserDao;
import com.gsm.dto.RegisterUserDto;
import com.gsm.entity.RespCode;
import com.gsm.entity.Result;
import com.gsm.po.WebUser;
import com.gsm.service.RegisterService;
import com.gsm.utils.Base64Utils;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterDao registerDao;
    @Autowired
    private WebUserDao webUserDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result insertUserInfo(RegisterUserDto dto) {
        WebUser webUser = registerDao.selectUserByIphoneNo(dto.getIphoneNo());
        if (webUser != null) {
            return Result.error(RespCode.REG_REGISTERED);
        }
        WebUser user = WebUser.builder()
                .webUserId(idUtils.nextId())
                .userName(dto.getIphoneNo())
                .po(Base64Utils.base64Encode(dto.getPo()))
                .pc(passwordEncoder.encode(dto.getPc()))
                .iphoneNo(dto.getIphoneNo())
                .vip(0)
                .headPortrait("https://bkimg.cdn.bcebos.com/pic/cefc1e178a82b901d3ed95f0748da9773812efb5?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U4MA==,g_7,xp_5,yp_5")
                .createDate(new Date())
                .updateDate(new Date())
                .status(1)
                .build();
        webUserDao.insertWebUser(user);
        return Result.success();
    }
}
